﻿using VIFON.BLL;
using VIFON.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Linq;

namespace VIFON
{
    public partial class frmCarId : Telerik.WinControls.UI.RadForm
    {
        public frmCarId()
        {
            InitializeComponent();
        }
        BLCar blCar = new BLCar();
        private void LoadCarList()
        {
            grvcarId.Rows.Clear();
            var list = blCar.GetCarList();
            foreach(var l in list)
            {
                grvcarId.Rows.Add(l.CarId);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(txtcarId.Text=="")
            {
                MessageBox.Show("Nhập biển số xe", "Thông báo");
                txtcarId.Focus();
            }
            else
            {
                Car boCar = new Car
                {
                    CarId=txtcarId.Text
                };
                if (blCar.Insert(boCar))
                    MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                LoadCarList();
            }
        }


        private void frmCarId_Load(object sender, EventArgs e)
        {
            LoadCarList();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (blCar.Delete(txtcarId.Text))
                MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            LoadCarList();
            txtcarId.Text = string.Empty;
        }

        private void grvcarId_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            try
            {
                int rowindex = grvcarId.CurrentCell.RowIndex;
                txtcarId.Text = grvcarId.Rows[rowindex].Cells[0].Value.ToString();
            }
            catch { }     
        }
    }
}
